package com.ocp.macro.controller;

import com.alibaba.fastjson.JSONObject;
import com.ocp.macro.entity.AlctProduct;
import com.ocp.macro.entity.Allocation;
import com.ocp.macro.service.AllocationService;
import com.ocp.macro.service.AllocationShowService;
import com.ocp.macro.utils.GsonUtils;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 调拨单控制层
 */
@CrossOrigin
@RestController
@Secured({"ROLE_supperAccount"})
public class AllocationController {

    @Resource
    private AllocationService allocationService;

    @Resource
    private AllocationShowService allocationShowService;
    /**
     * 调拨定制单预加载页面信息
     */
    @PostMapping("/macro/alct/alctprepareinfo")
    public String loadingInfoCon(){
        return allocationService.loadingInfo(null);
    }

    /**
     * 调拨仓库信息
     * @param orgId 机构id
     * @return json 仓库
     */
    @PostMapping("/macro/alct/deposinfo")
    public String allocationDeposInfo(Integer orgId){
        return allocationService.deposInfo(orgId);
    }

    /**
     * 送货地址信息
     * @param dealerListId  经销商id
     * @return 返回送货地址信息\关联备货订单编号json
     */
    @PostMapping("/macro/alct/addrlist")
    public String allocationAddressList(Integer dealerListId){
        return allocationService.addressList(dealerListId);
    }

    /**
     * 送货信息详情
     * @param addrId 送货地址id
     * @return 返回送货详细信息json
     */
    @PostMapping("/macro/alct/addrinfo")
    public String allocationAddressInfo(Integer addrId){
        return allocationService.addressInfo(addrId);
    }

    /**
     * 在仓库库位表中查询产品编号
     *@return 产品编号json
     */
    @PostMapping("/macro/alct/alctdppdt")
    public String allocationDeposProduct(String deposNo){
        return allocationService.deposProductNo(deposNo);
    }

    /**
     *
     * 查看出库和入库的库位详细信息
     * @param data proNo 产品编号  deposNo 仓库编号
     * @return 返回查看出库和入库的库位详细信息
     */
    @PostMapping("/macro/alct/alctdplace")
    public String allocationDeposPlace(@RequestBody String data){
        JSONObject jsonObject = GsonUtils.toJsonObject(data);
        return allocationService.deposPlacesAll(jsonObject.getString("proNo"),jsonObject.getString("indeposNo"),jsonObject.getString("outdeposNo"));
    }

    /**
     * 查询备货订单详细信息
     * @param stockNo 备货订单编号
     * @return 备货订单详细信息
     */
    @PostMapping("/macro/alct/alctsrel")
    public String allocationStockRelation(String stockNo){
        return allocationService.manyStocks(stockNo);
    }


    /**
     *生成调拨单
     * @param data Allocation allocation 调拨单数据  AlctProduct[] alctProduct 调拨产品数据
     * @return
     */
    @PostMapping("/macro/alct/alctcreate")
    public String allocationCreate(@RequestBody String data) {
        JSONObject jsonObject = GsonUtils.toJsonObject(data);
        Allocation allocation = GsonUtils.toBean(jsonObject.getString("allocation"), Allocation.class);
        List<AlctProduct> alctProduct = GsonUtils.toList(jsonObject.getString("alctProduct"), AlctProduct.class);
        return allocationService.allocationCreate(allocation,alctProduct);
    }

    /**
     * 动态查询调拨单详细信息
     * @param data 动态查询条件 当前页数
     * @return 调拨单信息
     */
    @PostMapping("/macro/alct/acltlist")
    public String allocationSelectList(@RequestBody String data){
        JSONObject jsonObject = GsonUtils.toJsonObject(data);
        Allocation allocation = GsonUtils.toBean(jsonObject.getString("allocation"), Allocation.class);
        return allocationShowService.showConditionAllocation(allocation);
    }

    /**
     * 查询一条调拨单详细信息
     * @param alctNo 调拨单号
     * @return 调拨单信息
     */
    @PostMapping("/macro/alct/acltlistone")
    public String allocationSelectListone(String alctNo){
        return allocationShowService.showConditionAllocationOne(alctNo);
    }

    /**
     * 调拨单中调拨产品信息
     * @param data 调拨单编号 和第几页
     * @return 多个产品信息
     */
    @PostMapping("/macro/alct/acltprolist")
    public String allocationSelectProductList(@RequestBody String data){
        JSONObject jsonObject = GsonUtils.toJsonObject(data);
        return allocationShowService.showConditionAlctProduct(jsonObject.getString("alctNo"),jsonObject.getInteger("page"));
    }

    /**
     * 查询备货订单编号
     */
   @PostMapping("/macro/alct/stockno")
    public String selectstockNoRest(){
        return allocationService.selectstockNo();
    }
}
